import os
import time
import re
#获取当前tomcat的进程
tomcat_return = os.popen('ps -ef | grep tomcat').readlines()
print("查看当前tomcat的进程")
print(tomcat_return)
print("开始关闭tomcat")
close_tomcat = os.popen('/usr/local/apache-tomcat-7.0.69/bin/shutdown.sh').readlines()
print(close_tomcat)
#有时候进程关闭不了，需要kill进程
for i in range(len(tomcat_return)):
    tempps = tomcat_return[i]
    t = re.search('/usr/local/jdk',tempps)
    if t != None:
        lt = tempps.split(' ')
        tl = []
        for j in range(len(lt)):
            if lt[j] != '':
                tl.append(lt[j])
        os.system('kill -9 '+tl[1])

#当前线程休眠5秒
time.sleep(5)
#启动tomcat
print("开始启动tomcat==========================================")
start_tomcat = os.popen('/usr/local/apache-tomcat-7.0.69/bin/startup.sh').readlines()
time.sleep(5)
#查看当前tomcat进程
print("显示当前tomcat进程")
tomcat_return = os.popen('ps -ef | grep tomcat').readlines()
print(tomcat_return)

